﻿--lua script
--Do something about space copy
--LocalDT.TeamCopyMap = {}
--LocalDT.PlayerDieCopyMap = {}


--#include "data\functions\FuBenNpcFunc\OnEnterFubenFunc.lua" once
--#include "data\functions\FuBenNpcFunc\FubenCommonFunc.lua" once
--#include "data\functions\Boss\PersonBossCommon.lua" once
--#include "data\functions\Activity\DailyActivity\SecretTreasure.lua" once 	--龙城秘宝
--#include "data\functions\Activity\DailyActivity\NightFighting.lua" once	--夜战皇城
--#include "data\functions\Vip\VipSceneCommon.lua" once   					--vip挂机
--#include "data\functions\FuBen\AllDay.lua" once							--屠龙深渊（闯天关）
--#include "data\functions\Boss\DreamLandBossCommon.lua" once 				--幻境boss
--#include "data\functions\Fuben\SkyTower.lua" once							--通天塔
--#include "data\functions\Activity\DailyActivity\BaoZangActivity.lua" once					--宝藏传说
--#include "data\functions\Activity\DailyActivity\SupplyContention.lua" once				--补给争夺

--（没用到）  用于进入队伍副本的条件判断
local enterTeamFubenF = function (sysarg,arg)
	--local FbID =arg[1]     --副本的ID
	local FbID =tonumber(arg[1])
	--print("FbID:"..FbID)
	local fbmode = GetMode(sysarg,FbID)
	--print("fbmode:"..fbmode)
	local TeamMemb = LuaHelp.getTeamMemberList(sysarg)		--获取该队伍的成员
	if TeamMemb == nil then return end
	
	--print("type:"..table.getn(TeamMemb))
	--print(type(TeamMemb[1]))

	local actorid = Actor.getIntProperty(TeamMemb[1],PROP_ENTITY_ID)
	--print("actorid:"..actorid)
	
	local fbdata = FuBen[FbID]
	
	for i = 1,table.getn(TeamMemb) do
		local memblevel = Actor.getIntProperty(TeamMemb[i],PROP_CREATURE_LEVEL) --获取玩家的等级
		--print("memblevel:"..memblevel)
		local membfbcount = Actor.getDayFubenCount(TeamMemb[i],fbmode)   --获取玩家进入副本的剩余次数
		--print("membfbcount:"..membfbcount)
		if memblevel < fbdata.minLevel then
			Actor.sendTipmsg(sysarg,Lang.ScriptTips.f00006,ttDialog)
			return false
		end
		if membfbcount < 1 then
			Actor.sendTipmsg(sysarg,Lang.ScriptTips.f00006,ttDialog)
			return false
		end
	end
	
	if ( fbdata == nil ) then
		--print("Enter team fu ben envir config empty."..FbID)
		return
	end
	if table.getn(TeamMemb) < fbdata.minPlayCount then		--判断队伍的人数是否满足条件
		                                            		--队伍人数不够，不能进入副本。
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.f00001,ttDialog)
		return false
	end
	
	if table.getn(TeamMemb) >fbdata.maxPlayCount then
		--Actor.sendTipmsgWithId(sysarg,tpTeamMemberFull) 		--队伍人数超过最多人数，不能进入副本
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.f00002,ttDialog)
		return false
	end

   Hfuben = Fuben.createFuBen(FbID)
	for i = 1,table.getn(TeamMemb) do		--如果满足条件将队伍的成员逐个传送到副本场景				
		Actor.enterFuBen(TeamMemb[i],Hfuben,-1,-1,-1)
		--print("enterFuBen:")
		Actor.subDaySingleCount(TeamMemb[i],fbmode)	--并将当日进入相应副本的次数减1
		Actor.closeDialogs(TeamMemb[i],diFubenTeam)
		local copyEnterFunc = OnEnterCopyMap[FbID]	--进入各种副本的个性化处理
		if ( copyEnterFunc ~= nil ) then copyEnterFunc( sysarg,arg, Hfuben ) end
	end
end
--判断能否使用副本挑战书 
function canUseFubenChallengeBook(sysarg)
  
end


--退出副本的函数
function exitFubenFunc(sysarg)
	print("++++++++++++++退出副本的函数 exitFubenFunc");
  local FbID = Actor.getFubenId(sysarg)
  local TeamMemb = LuaHelp.getTeamMemberList(sysarg)		--获取该队伍的成员
  local TeamCount = Actor.getOnlineTeamMemberCount(sysarg)
  --local TeamCount = table.getn(TeamMemb)
  --print("TeamCount:")
  --print("FbID-----------:"..FbID)
  local fbdata = FuBen[FbID]
  local mode = GetMode(sysarg,FbID)
  ----print("mode:"..mode)
  if mode == fmCommon then			--如果是在普通场景，则什么都不做！
   return
  end
  if mode == fmSingle then			--如果是单人副本，直接退出副本
   Actor.exitFuben(sysarg)
  else
   if Actor.isInTeam(sysarg) then
    if TeamCount <= fbdata.minPlayCount then
    	for i=1 ,TeamCount do
    	Actor.exitTeam(TeamMemb[i])				--如果是队伍副本，就退出队伍
    	Actor.exitFuben(TeamMemb[i])
    	end
    else
    	Actor.exitTeam(sysarg)
      Actor.exitFuben(sysarg)
    end
   else 
      Actor.exitFuben(sysarg)
   end

    Actor.exitTeam(sysarg)				--如果是队伍副本，就退出队伍
  end
  Actor.clearKillMonsterCount(sysarg)
end


--取消创建副本
function cancelCreateFuben(sysarg)

end
--创建副本时有队员不符合条件，踢掉队员
function getoutTeamMember(sysarg,FbID)
	local FbId = tonumber(FbID) 
	
	--print("fbid:"..FbId)
	--local FbMode = 	GetMode(sysarg,FbId)    --获取副本的类型
	local TeamMemb = LuaHelp.getTeamMemberList(sysarg)		--获取该队伍的成员
	if TeamMemb == nil then return end
	local fbdata = FuBen[FbId]
	local groupid = fbdata.groupId
	for i = 1,table.getn(TeamMemb) do
		local memblevel = Actor.getIntProperty(TeamMemb[i],PROP_CREATURE_LEVEL) --获取玩家的等级
		local var = Actor.getStaticVar(TeamMemb[i])
    local fb = var.fb
    local membfbcount = fb[groupid]      --获取玩家进入副本的剩余次数
    
	local memFbAddCount = 0
	if (fbdata.fbtype == 0) then
		memFbAddCount = getFBAddCount(TeamMemb[i])
	end
	membfbcount = membfbcount + memFbAddCount
		
		--local membfbcount = Actor.getDayFubenCount(TeamMemb[i],FbMode)   --获取玩家进入副本的剩余次数
		if (memblevel < fbdata.minLevel or membfbcount < 1 )then
			Actor.exitTeam(TeamMemb[i])	
		end
	end
	Actor.createFubenTeam(sysarg,FbId)
end


function EventCallDispatcher.TeamTimer( sysarg, arg )
	local teamId = tonumber(arg[1])
	local captainPtr = TeamFun.getTeamCaptain( teamId )
	if captainPtr then
		local var = TeamFun.getDyanmicVar(teamId)
		local fbId = var.enterFbId
		if fbId then
			enterTeamFbByLeader( captainPtr, fbId )
		end
	end
end

function EventCallDispatcher.handlerExitFuben( sysarg, arg )
	local fubenId = tonumber(arg[1])
	local curFbId = Actor.getFubenId(sysarg)
	if curFbId ~= fubenId then
		return
	end
	
	onExitFubenFunc(sysarg)
end

function EventCallDispatcher.handGetFBTimes(sysarg, arg )
	SendEnterFbTimes(sysarg)
end

--[[
function EventCallDispatcher.handPataFubenMasterAward( sysarg, arg )
	local bType = arg[1]
	local fubenId = arg[2]
	return GiveMyMasterAward( sysarg, bType, fubenId)
end
]]

function EventCallDispatcher.handStoryFBItem( sysarg, arg )
	OnGetStoryFbItem(sysarg)
end

function EventCallDispatcher.handBuyRandNpcItem( sysarg, arg )
	local idx = arg[1]
	local itemId = arg[2]
	local count = arg[3]
	OnBuyNpcShopItem( sysarg, idx,itemId, count)
end

--[[
副本时间到
]]
EventCallDispatcher.OnFubenTimeout = function( fubenPtr, arg)
	--sysarg是副本指针,获取副本所有玩家的指针，并让其退出副本
	local fubenId = arg[1]
	--print("OnFubenTimeout, fubenId="..fubenId)
	--[[
	if IsInPersonBossFuben( fubenId ) then
		OnPersonBossTimeout( fubenPtr )
		return
	end
	]]
	--[[
	执行某些功能对于副本时间到的特殊要求
	]]
	if FubenTimeoutDispatcher.dispatch( fubenId, fubenPtr ) then
		return
	end

	--如无特殊处理，则T出副本中所有玩家
	local actorList = LuaHelp.getFbActorList( fubenPtr )
	for k, ptr in ipairs(actorList or {}) do
		onExitFubenFunc(ptr, true)
	end
end


--[[
通用副本/活动面板的退出响应
可能退出副本或者活动
]]
function EventCallDispatcher.handlerCommonRightPanelQuit(sysarg, arg )
	local panelType = arg[2]
	local sceneId 	= Actor.getSceneId( sysarg )
	local actorId 	= Actor.getActorId( sysarg )
	local actorName = Actor.getName( sysarg )
	local sceneName = Fuben.getSceneNameById(sceneId)
	--print( "handlerCommonRightPanelQuit, panelType="..panelType )
	if panelType == 1 then			--退出副本
		local AllDayState = false
		for iindex = 1, #AllDayCfg do
			for idx = 1, #AllDayCfg[iindex].Checkpoint do
				if Actor.getFubenId(sysarg) == AllDayCfg[iindex].Checkpoint[idx].FbId then
					if Actor.getSceneId(sysarg) == AllDayCfg[iindex].Checkpoint[1].EnterPos[1] then
						AllDayState = true
						break
					end
				end
			end
		end

		SceneHandExitDispatcher.dispatch( sysarg )			--触发手工离开场景的逻辑
		System.sendCommonLog( GameLog.clExitByHand, actorId, actorName, sceneId, panelType, 0,'','', sceneName)
		if AllDayState then
			AllDayFinishHandler(sysarg)
		else
			Actor.exitFuben(sysarg) 	--正式退出副本，在此之前都是在副本中
		end
	elseif panelType == 2 then		--退出场景
		SceneHandExitDispatcher.dispatch( sysarg )				--触发手工离开场景的逻辑
		System.sendCommonLog( GameLog.clExitByHand, actorId, actorName, sceneId, panelType, 0,'','', sceneName)
		--BackToMainCity( sysarg )	--默认返回主城
		if sceneId == VipSceneConfig.EnterPos[1][1] then
			VipSceneLeaveActivity(sysarg)
			return
		--elseif sceneId == SecretTreasureCfg.EnterPos[1][1] then
		--	SecretTreasureLeaveActivity(sysarg)
		--	return
		--elseif sceneId == NightFightingCfg.EnterPos[1][1] then
		--	NightFightingLeaveActivity(sysarg)
		--	return 
		--elseif sceneId == DreamLandBossConfig.EnterPos[1] then
		--	DreamLandBossLeaveActivity(sysarg)
		--	return 
		--elseif sceneId == MoZunConfig.sceneId then
		--	OnExitMoZun(sysarg)
		--	return
		elseif sceneId == BaoZangConfig.EnterPos[1][1] then
			BaoZangLeaveActivity(sysarg)
			return 
		elseif sceneId == SupplyContentionConfig.unionTeams[1].EnterPos[1] then
			SupplyContentionLeaveActivity(sysarg)
			return
		end
		Actor.returnCity(sysarg)
	end
end

--[[
通用副本/活动面板的退出响应
可能退出副本或者活动
]]
function EventCallDispatcher.handlerCommonRightPanelAward(sysarg, arg )
	local awardRate = arg[2]			--奖励倍率，1-单倍，2-双倍
	--print( "handlerCommonRightPanelAward, awardRate="..awardRate )
	SceneAwardDispatcher.dispatch( sysarg, awardRate  )
end

--[[
[通用活动]请求某个后台活动的数据	（活动具体数据，由每个功能定协议，自行发送）
]]
function EventCallDispatcher.handlerCommonActivityGetData(sysarg, arg )
	local activityId = arg[2]			--活动ID
	local funcId 	 = arg[3]			--活动ID
	ActivityGetDataDispatcher.dispatch( sysarg, activityId, funcId  )
end


EventCallDispatcher.readyEnterFB = readyEnterFB
EventCallDispatcher.registerEventCall(BaseTypes.ActorEventId.aeTeamTimer, EventCallDispatcher.TeamTimer)
EventCallDispatcher.registerEventCall(BaseTypes.ActorEventId.aeEnterFuben,EventCallDispatcher.readyEnterFB)

EventCallDispatcher.registerEventCall(BaseTypes.ActorEventId.aeExitFuben,EventCallDispatcher.handlerExitFuben)
EventCallDispatcher.registerEventCall(BaseTypes.ActorEventId.aeEnterFbTimes, EventCallDispatcher.handGetFBTimes)
--EventCallDispatcher.registerEventCall(BaseTypes.ActorEventId.aeGetPaTaMasterAward, EventCallDispatcher.handPataFubenMasterAward)
EventCallDispatcher.registerEventCall(BaseTypes.ActorEventId.aeGetStoryFBItem, EventCallDispatcher.handStoryFBItem)
--EventCallDispatcher.registerEventCall(BaseTypes.ActorEventId.aeBuyNpcShopItem, EventCallDispatcher.handBuyRandNpcItem)
--EventCallDispatcher.registerEventCall(BaseTypes.ActorEventId.aeMoGoogMopOp, EventCallDispatcher.handMoGoogMopOp)	--魔攻扫荡（不使用）


EventCallDispatcher.registerEventCall(aeCommonRightPanelAward, EventCallDispatcher.handlerCommonRightPanelAward)	--在用(2017.2)
EventCallDispatcher.registerEventCall(aeCommonRightPanelQuit, EventCallDispatcher.handlerCommonRightPanelQuit)		--在用(2017.2)
EventCallDispatcher.registerEventCall(aeCommonActivityGetData, EventCallDispatcher.handlerCommonActivityGetData)	--在用(2017.2)
EventCallDispatcher.registerEventCall(aeFubenTimeOut, EventCallDispatcher.OnFubenTimeout)							--在用(2017.2)
